package org.bao.netcrawler.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


/**
 * 
 * write String to a file.
 * @author Bao<bb@feijiao.info>
 * @since 2010-12-24
 * @version 0.9.0
 * @license GPLv3
 */

public class Writer {
	public static final String FILE_PATH_DEFAULT = "result.txt";
	
	
	public static void out(String content){
		Writer.out("./" + Cal.getDateTime() + "_" + Writer.FILE_PATH_DEFAULT,content);
	}
	
	public static void out(String path, String content){
		Writer.out(path,content,false);
	}
	
	public static void out(String path, String content, boolean append){
		FileWriter fw = null;
		BufferedWriter bw = null;
		
		try {
			File file = new File(path);
			
			if(!file.exists()){
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			
			fw = new FileWriter(file,append);
			bw = new BufferedWriter(fw);
			
			bw.write(content);
			bw.flush();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(bw != null){
					bw.close();
				}
				
				if(fw != null){
					fw.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}
